package com.zjpavt.client.command.entity;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.zjpavt.client.util.ClientConfig;
import com.zjpavt.client.util.PropertiesUtil;

/**
 * 视频服务器类
 * @author 骏
 */
public class VideoServer {

    private final static VideoServer instance = new VideoServer();

    public static final int VIDEO_NUM=8;//视频播放选节目二进制8位数

    private VideoServer() {
    }

    public static VideoServer getInstance() {
        return instance;
    }

    //指定播放节目序号0开始：000~127
    private String video;
    //播放速度：1-8
    private String speed;
    //亮度：000-225
    private String bright;
    //播放模式：0-4，0-循环、1-单一、2-定时、3-同步、4-停止
    private String mode;
    //节目数量
    private String videosum;
    //播放时间
    private String times;
    //节目单
    private String timingProgramGuides;

    private List<Play> playList;

    /**
     * 获取指定播放节目序号0开始
     * @return
     */
    public String getVideo() {
        try {
            if(video == null)
                video = PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "video", "000");
            return video;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存指定播放节目序号0开始
     * @return
     */
    public void setVideo(String video) {
        try {
            if("0".equals(mode) || "1".equals(mode)) { //实时切换节目只有在单一模式或循环模式下才能切换
                //怕初始化的时候节目数量为空有延迟
                while(true) {
                    if(this.videosum == null)
                        this.videosum=getVideosum();//如果节目数量为空，从配置文件里面取
                    if(this.videosum != null && Integer.valueOf(this.videosum)>Integer.valueOf(video)) { //如果节目数量不为空，并且节目数大于等于指定播放节目才可以
                        this.video = video;
                        PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "video",video);
                    }
                    if(this.videosum != null)
                        break;//如果节目数量有值就不循环
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取播放速度
     * @return
     */
    public String getSpeed() {
        try {
            if(speed == null)
                speed = PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "speed", "5");
            return speed;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存播放速度
     * @return
     */
    public void setSpeed(String speed) {
        try {
            if(!speed.equals(this.speed)) {
                PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "speed", speed);
                this.speed = speed;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取亮度
     * @return
     */
    public String getBright() {
        try {
            if(bright == null)
                bright = PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "bright", "255");
            return bright;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存亮度
     * @return
     */
    public void setBright(String bright) {
        try {
            if(!bright.equals(this.bright)) {
                this.bright = bright;
                PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "bright", bright);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取指播放模式
     * @return
     */
    public String getMode() {
        try {
            if(mode == null)
                mode = PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "mode", "1");
            return mode;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存播放模式
     * @return
     */
    public void setMode(String mode) {
        try {
            if(!mode.equals(this.mode)) {
                this.mode = mode;
                PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "mode", mode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取节目数量
     * @return
     */
    public String getVideosum() {
        try {
            if(videosum == null)
                videosum=PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "videosum", "000");
            return videosum;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存节目数量
     * @return
     */
    public void setVideosum(String videosum) {
        try {
            if(!videosum.equals(this.videosum)) {
                this.videosum = videosum;
                PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "videosum", videosum);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getTimes() {
        return times;
    }

    public void setTimes(String times) {
        this.times = times;
    }

    public String getTimingProgramGuides() {
        try {
            if(timingProgramGuides == null)
                timingProgramGuides=PropertiesUtil.getProperties(ClientConfig.VIDEO_SERVER, "timingProgramGuides", null);
            return timingProgramGuides;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void setTimingProgramGuides(String timingProgramGuides) {
        try {
            if(!timingProgramGuides.equals(this.timingProgramGuides)) {
                this.timingProgramGuides = timingProgramGuides;
                PropertiesUtil.setPropertyOrReplace(ClientConfig.VIDEO_SERVER, "timingProgramGuides", timingProgramGuides);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<Play> getPlayList() {
        return playList;
    }

    public void setPlayList(List<Play> playList) {
        this.playList = playList;
    }

    public Play getPlay() {
        return new Play();
    }

    public class Play {
        private Date timeBegin;
        private Date timeEnd;
        private String videos;

        public Date getTimeBegin() {
            return timeBegin;
        }
        public void setTimeBegin(Date timeBegin) {
            this.timeBegin = timeBegin;
        }
        public Date getTimeEnd() {
            return timeEnd;
        }
        public void setTimeEnd(Date timeEnd) {
            this.timeEnd = timeEnd;
        }
        public String getVideos() {
            return videos;
        }
        public void setVideos(String videos) {
            this.videos = videos;
        }
    }

}
